package com.gxa.wmcrm.Action;

import com.gxa.wmcrm.entity.User;
import com.gxa.wmcrm.service.Impl.UserServiceImpl;
import com.gxa.wmcrm.service.UserService;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

import static com.opensymphony.xwork2.Action.SUCCESS;

public class LoginActions {
    public Map<String, Object> getResultMap() { return resultMap; }

    private Map<String,Object> resultMap;

    public String execute(){
        System.out.println("--LoginAction--");
        return SUCCESS;
    }
    public String login(){
        System.out.println("--LoginAction login");
        //1.获取request对象
        HttpServletRequest request = ServletActionContext.getRequest();
        //2.获取Ajax提交的json ParameterMap
        Map<String,String[]> map = request.getParameterMap();

        //用户名，密码，验证码，系统验证码
        String username = map.get("login")[0];
        String password = map.get("pwd")[0];
        String code = map.get("code")[0];
        String AdminCode = map.get("AdminCode")[0];

        System.out.println("-- username -- : " + username);
        System.out.println("-- password -- : " + password);
        System.out.println("-- code -- : " + code);
        System.out.println("-- AdminCode -- : " + AdminCode);

        //将需要被提交的数据封装到Userbean
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);

        //准备返回数据-Map
        resultMap = new HashMap<>();


        //判断用户输入的验证码是否正确
        if(code.equals(AdminCode)){
            UserService userService = new UserServiceImpl();
            int action = userService.queryUser(user);
            if (action == 1) {
                // 准备返回数据 - Map
                resultMap.put("Status","success");
                resultMap.put("Text","登录成功!");
            } else {
                resultMap.put("Status","error");
                resultMap.put("Text","登录失败！");
            }
        } else {
            resultMap.put("Status","error");
            resultMap.put("Text","验证码错误！");
        }
        return SUCCESS;
    }
}
